package de.jmda.mproc.swingui;               

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
                                                                                       
/**                                                                                    
 * This class was initially generated by {@link de.jmda.gui.swing.mvc.ControllerSupportEnabledGeneratorJV6}.    
 * <p>                                                                                 
 * <code>ControllerSupportEnabledGeneratorJV6</code> will not regenerate this class,
 * hence modifications will not get lost due to subsequent generation by <code>ControllerSupportEnabledGeneratorJV6</code>.                                         
 */                                                                                    
class JPanelVisualProcessorConfigurationController                                                          
		extends de.jmda.gui.swing.mvc.AbstractController<JPanelVisualProcessorConfigurationModel>          
{
	private JPanelVisualProcessorConfigurationControllerContext context;                                   
                                                                                       
  private JPanelVisualProcessorConfigurationModel model;
  
	/**                                                                                   
	 * Constructor                                                                        
	 */                                                                                   
	JPanelVisualProcessorConfigurationController(                                                              
			de.jmda.gui.swing.mvc.ControllerContextProvider controllerContextProvider)      
	{                                                                                     
		super(controllerContextProvider);
		refreshControllerContext();
	}                                                                                     
                                                                                       
	@Override                                                                             
	public void refreshControllerContext()                                                
	{                                                                                     
		context =
				(JPanelVisualProcessorConfigurationControllerContext)
						controllerContextProvider.getControllerContext();
	}                                                                                     
                                                                                       
	@Override                                                                             
	public void setModel(JPanelVisualProcessorConfigurationModel model)                                    
	{                                                                                     
		this.model = model;

		updateFromModelJListConfigurations();
	}

	private void updateFromModelJListConfigurations()
  {
		JList jList = context.getJListConfigurations();
		
		if (jList == null)
		{
			// jList is not initialised yet, leave
			return;
		}
		
		List<JPanelVisualProcessorModel>
				jPanelVisualProcessorModels =
						model.getLaunchAnnotationProcessingModels();
		
		// populate context.jListConfigurations
		DefaultListModel defaultListModel = new DefaultListModel();
		
		for (
				JPanelVisualProcessorModel
						jPanelVisualProcessorModel :
				jPanelVisualProcessorModels)
		{
			defaultListModel.addElement(
					jPanelVisualProcessorModel.getConfigurationName());
		}
		
		jList.setModel(defaultListModel);

//JOptionPane.showInputDialog(jList, "jlist values", "debugging jlist", 0, null, defaultListModel.toArray(), null);
		
		if (defaultListModel.getSize() > 0)
		{
			jList.setSelectedIndex(0);
		}
		
//		jList.repaint();
  }

	public void newActionPerformed(ActionEvent evt)
  {
		String newConfigurationName =
				JOptionPane.showInputDialog(
						context.getJPanelLaunchAnnotationProcessingConfiguration(),
						"new launch annotation processing configuration name");
		
		List<JPanelVisualProcessorModel>
				jPanelVisualProcessorModels =
						model.getLaunchAnnotationProcessingModels();

		boolean nameAlreadyExists = false;
		
		for (
				JPanelVisualProcessorModel
						jPanelVisualProcessorModel :
				jPanelVisualProcessorModels)
		{
			if (jPanelVisualProcessorModel.getConfigurationName()
			                                         .equals(newConfigurationName))
			{
				nameAlreadyExists = true;
				break;
			}
		}
		
		if (nameAlreadyExists)
		{
			JOptionPane.showMessageDialog(
					context.getJPanelLaunchAnnotationProcessingConfiguration(),
					newConfigurationName + " already exists");
			
			return;
		}
		
		JPanelVisualProcessorModel
				jPanelLaunchAnnotationProcessingModelNew =
						new JPanelVisualProcessorModel(newConfigurationName);
		
		// update model
		model.getLaunchAnnotationProcessingModels()
         .add(jPanelLaunchAnnotationProcessingModelNew);
//		jPanelLaunchAnnotationProcessingModels.add(
//				jPanelLaunchAnnotationProcessingModelNew);
		// update view
		updateFromModelJListConfigurations();
  }

	public void deleteActionPerformed(ActionEvent evt)
  {
		int selectedIndex = context.getJListConfigurations().getSelectedIndex();
		
		if (selectedIndex > -1)
		{
			model.getLaunchAnnotationProcessingModels().remove(selectedIndex);
			// update view
			updateFromModelJListConfigurations();
		}
  }

	public void saveActionPerformed(ActionEvent evt)
  {
		model.save();
  }

	public void configurationsValueChanged(ListSelectionEvent evt)
  {
		int selectedIndex = context.getJListConfigurations().getSelectedIndex();
		
		if (selectedIndex > -1)
		{
			List<JPanelVisualProcessorModel>
					jPanelVisualProcessorModels =
							model.getLaunchAnnotationProcessingModels();

			JPanelVisualProcessorModel
					jPanelVisualProcessorModel =
							jPanelVisualProcessorModels.get(selectedIndex);

			JPanel jPanel = context.getJPanelRight();
			
//		if (jPanel == null)
//		{
//			// jPanel is not initialised yet, leave
//			return;
//		}

			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.fill = GridBagConstraints.BOTH;
	
			JPanelVisualProcessor jPanelVisualProcessor =
			    new JPanelVisualProcessor(
			    		jPanelVisualProcessorModel);
	
			jPanel.removeAll();
			jPanel.add(jPanelVisualProcessor, gridBagConstraints);
			
			jPanel.revalidate();
			jPanel.repaint();
		}
  }                                                                                     
}